package com.hzcx.app.simplechat.api;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 *  服务器错误返回类
 */
public class HttPErrorUtils {

    /**
     *  服务器错误信息解析
     * @param error
     * @return
     */
    public static String getHttpErrorStr(String error) {
        String showText = "";
        try {
            JsonObject errJson = new Gson().fromJson(error, JsonObject.class);
            if (errJson.has("error")) {
                JsonObject rst = errJson.getAsJsonObject("error");
                if (rst.has("reason") && rst.has("message")) {
                    showText = rst.get("reason").getAsString() + "\n" + rst.get("message").getAsString();
                } else if (rst.has("message")) {
                    showText = rst.get("message").getAsString();
                } else if (rst.has("reason")) {
                    showText = rst.get("reason").getAsString();
                }
            }
        } catch (Exception e) {
            showText = error;
        }

        return showText;
    }

}
